﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoFix.Base;
using BusinessLogicLayer.Manager;
using Objects;
using AutoFix.controls;
using AutoFix.ClientComponents;
using Common;

namespace AutoFix
{
    public partial class login : BasePage
    {
        #region [Private Members]

        /// <summary>
        /// The page session
        /// </summary>
        //private AppSession PageSession = new AppSession();

        #endregion

        #region [BasePage Implementations]
        
        /// <summary>
        /// Gets or sets a value indicating whether [is login page].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is login page]; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsLoginPage { get { return true; } }

        /// <summary>
        /// Gets or sets a value indicating whether [is anonymous page].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is anonymous page]; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsAnonymousPage { get { return true; } }

        /// <summary>
        /// Page name
        /// </summary>
        protected override string PageName
        {
            get { return "Login"; }
        }
        /// <summary>
        /// Gets the authorized user types.
        /// All users are allowed to access the login page
        /// </summary>
        /// <value>
        /// The authorized user types.
        /// </value>
        protected override List<UserType> AuthorizedUserTypes
        {
            get { return new List<UserType>(new UserType[] { UserType.None }); }
        }
      
        #endregion

        #region [Event Handlers]

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string email = this.txtEmailAddress.Text.Trim();
                string password = Encryption.Encrypt(this.txtPassword.Text.Trim());

                UserManager manager = new UserManager();
                User user = manager.AuthenticateUser(email, password);

                if (user == null)
                {
                    this.pageActionMessage.InnerText = "Email Address and Password is invalid. Try again.";
                    this.pageActionMessage.Attributes.Add("class", "information-msg");
                    return;
                }
                else if (user.IsActive == false)
                {
                    this.pageActionMessage.InnerText = "You have been deactivated from the system";
                    this.pageActionMessage.Attributes.Add("class", "information-msg");
                    return;
                }

                this.PageSession.ApplicationUser = user;

                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
                                                            email,
                                                            DateTime.Now,
                                                            DateTime.Now.AddMinutes(30),
                                                            false,
                                                            null);

                string authCookieStr = FormsAuthentication.Encrypt(authTicket);
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, authCookieStr);

                authCookie.Expires = authTicket.Expiration;
                authCookie.Path = FormsAuthentication.FormsCookiePath;
                Response.Cookies.Add(authCookie);

                FormsAuthentication.RedirectFromLoginPage(email, false);
            }
            catch (Exception eX)
            {

                this.pageActionMessage.InnerText = "Failed to authenticate your information";
                this.pageActionMessage.Attributes.Add("class", "error-msg");
            }
        }

        #endregion

    }
}